package interfaz;

import control.FileManager;
import funciones.Imagen;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/*
 * Interfaz.java
 *
 * Created on Apr 6, 2012, 11:01:28 AM
 */
/**
 *
 * @author Marvin
 */
public class Interfaz extends javax.swing.JFrame {

    /**
     * Creates new form Interfaz
     */
    public Interfaz() {
        initComponents();
        fm = new FileManager();
        img = null;
        escale = false;
        pintar_hist(Color.white);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Group_rotar = new javax.swing.ButtonGroup();
        Group_color = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jComboBoxRotar = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldRotar = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextFieldAncho = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldAlto = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jSliderEscalar = new javax.swing.JSlider();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jComboBoxCuantizar = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabelPeso = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabelDimensiones = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabelbpp = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        open = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setAutoscrolls(true);
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jLabel1);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));

        jButton1.setText("Escala de grises");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Ecualizar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jComboBoxRotar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-180", "-90", "-45", "45", "90", "180" }));

        jLabel5.setText("Rotar:");

        jLabel7.setText("º");

        jTextFieldRotar.setText("---");
        jTextFieldRotar.setEnabled(false);

        jLabel8.setText("Escalar:");

        jLabel9.setText("Ancho:");

        jTextFieldAncho.setText("100");

        jLabel10.setText("Alto:");

        jTextFieldAlto.setText("100");

        jLabel11.setText("px");

        jLabel12.setText("px");

        jSliderEscalar.setMaximum(1000);
        jSliderEscalar.setMinimum(1);
        jSliderEscalar.setValue(100);
        jSliderEscalar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jSliderEscalar.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderEscalarStateChanged(evt);
            }
        });

        jLabel13.setText("0%");

        jLabel14.setText("1000%");

        jLabel15.setText("Cuantizar:");

        jComboBoxCuantizar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "16", "8" }));

        jButton3.setText("Aceptar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Ver");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Aceptar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        Group_rotar.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Ángulo fijo");
        jRadioButton1.setToolTipText("");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        Group_rotar.add(jRadioButton2);
        jRadioButton2.setText("Ángulo arbritario");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Histograma");

        Group_color.add(jRadioButton3);
        jRadioButton3.setSelected(true);
        jRadioButton3.setText("Red");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        Group_color.add(jRadioButton4);
        jRadioButton4.setText("Green");
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        Group_color.add(jRadioButton5);
        jRadioButton5.setText("Blue");
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxCuantizar, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jButton5)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jSeparator4))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jButton4))
                            .addComponent(jRadioButton2))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jRadioButton3)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButton4)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButton5))
                            .addComponent(jRadioButton1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextFieldAlto, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton3))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel8)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextFieldAncho, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel11))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSliderEscalar, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel14))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(111, 111, 111)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jComboBoxRotar, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldRotar, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jComboBoxRotar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton2)
                    .addComponent(jTextFieldRotar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextFieldAncho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextFieldAlto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel13)
                    .addComponent(jSliderEscalar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jComboBoxCuantizar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton4)
                    .addComponent(jRadioButton5))
                .addContainerGap(73, Short.MAX_VALUE))
        );

        jRadioButton1.getAccessibleContext().setAccessibleName("rotar_notable");

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 86, Short.MAX_VALUE)
        );

        jLabel3.setText("Dimensiones:");

        jLabel4.setText("Peso de la imagen:");

        jLabelPeso.setText("xxx");

        jLabel18.setText("KB");

        jLabelDimensiones.setText("Ancho x Alto");

        jLabel17.setText("bpp:");

        jLabelbpp.setText("  ");

        jMenu1.setText("File");

        open.setText("Open");
        open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openActionPerformed(evt);
            }
        });
        jMenu1.add(open);

        jMenuItem2.setText("Save As...");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel18)
                        .addGap(81, 81, 81)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabelDimensiones)
                        .addGap(93, 93, 93)
                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelbpp, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 176, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabelPeso)
                    .addComponent(jLabel3)
                    .addComponent(jLabel18)
                    .addComponent(jLabelDimensiones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel17)
                    .addComponent(jLabelbpp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
}//GEN-LAST:event_jLabel1MouseClicked

    private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed

        File file = fm.cargarImagen(this);
        BufferedImage imagenEntrante = null;
        if (file != null) {
            try {
                imagenEntrante = ImageIO.read(file);
            } catch (FileNotFoundException fnfe) {
                JOptionPane.showMessageDialog(this, "Error abriendo el archivo");
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(this, "Error durante la lectura del archivo");
            }
        } else {
            return;
        }

        img = new Imagen(imagenEntrante);


        //Lo siguiente es la impresion de los datos de la imagen
        if (img != null) {
            DecimalFormat formatF = new DecimalFormat("0.000");
            float tam = file.length();
            jLabelPeso.setText(formatF.format(tam / 1024));
            jLabelDimensiones.setText(img.getWidthImagen() + " x " + img.getHeightImagen());
            jTextFieldAncho.setText(String.valueOf(img.getWidthImagen()));
            jTextFieldAlto.setText(String.valueOf(img.getHeightImagen()));
            jSliderEscalar.setValue(100);
            jLabelbpp.setText(String.valueOf(img.getBpp()));

            if (img.getBpp() < 24) {
                //pintar_hist(Color.gray);
                jRadioButton3.setText("Gray");
                jRadioButton4.setEnabled(false);
                jRadioButton5.setEnabled(false);
            } else {
                //pintar_hist(Color.red);
                jRadioButton3.setText("Red");
                jRadioButton4.setEnabled(true);
                jRadioButton5.setEnabled(true);
            }
        }

        //jLabel2.  ageIcon(salida));
        jLabel1.setIcon((new ImageIcon(img.imagenfinal())));
    }//GEN-LAST:event_openActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (img != null) {
            if (escale) {
                img = new Imagen(imgEscale);
                escale = false;
            }
            img.ConverToGray();
        } else {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una imagen");
        }
        jLabel1.setIcon(new ImageIcon(img.imagenfinal()));

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (img != null) {
            if (escale) {
                img = new Imagen(imgEscale);
                escale = false;
            }
            img.ecualizar();
        } else {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una imagen");
        }
        jLabel1.setIcon(new ImageIcon(img.imagenfinal()));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if (img != null) {
            if (escale) {
                img = new Imagen(imgEscale);
                escale = false;
            }
            escalar();
        } else {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una imagen");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jSliderEscalarStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderEscalarStateChanged

        if (img != null) {
            int newAlto = (int) ((img.getHeightImagen() * Float.valueOf(jSliderEscalar.getValue()) / 100));
            int newAncho = (int) ((img.getWidthImagen() * Float.valueOf(jSliderEscalar.getValue()) / 100));
            jTextFieldAlto.setText(Integer.toString(newAlto));
            jTextFieldAncho.setText(Integer.toString(newAncho));
            
            escalar();

        }
    }//GEN-LAST:event_jSliderEscalarStateChanged

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // Guardar l aimagen
        //fm.guardarImagen(this);
        if (img != null) {
            fm.guardarImagen(this, img.imagenfinal());
        } else {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una imagen");
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // Rotar:
        if (img != null) {
            if (escale) {
                img = new Imagen(imgEscale);
                escale = false;
            }
            double r = 0;
            if (jRadioButton1.isSelected()) {
                r = Math.toRadians(Double.parseDouble((String) jComboBoxRotar.getItemAt(jComboBoxRotar.getSelectedIndex())));
            } else {
                try {
                    r = Double.parseDouble(jTextFieldRotar.getText());
                    r = Math.toRadians(r % 360.0);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(this, "Valor o formato incorrecto, intente otra vez");
                }
            }
            rotar(r);
        } else {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una imagen");
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        int sel = Integer.parseInt((String) jComboBoxCuantizar.getItemAt(jComboBoxCuantizar.getSelectedIndex()));
        if (img != null) {
            if (escale) {
                img = new Imagen(imgEscale);
                escale = false;
            }
            img.converTo8bo16b(sel);
        } else {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una imagen");
        }
        jLabel1.setIcon(new ImageIcon(img.imagenfinal()));
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        jTextFieldRotar.setEnabled(false);
        jComboBoxRotar.setEnabled(true);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        jTextFieldRotar.setEnabled(true);
        jComboBoxRotar.setEnabled(false);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        if (img != null && img.getBpp() < 24) {
            pintar_hist(Color.gray);
        } else {
            pintar_hist(Color.red);
        }
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        if (img != null && img.getBpp() >= 24) {
            pintar_hist(Color.green);
        }
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
        if (img != null && img.getBpp() >= 24) {
            pintar_hist(Color.blue);
        }
    }//GEN-LAST:event_jRadioButton5ActionPerformed

    private void pintar_hist(Color c) {
        Dimension d = jPanel3.getSize();

        histograma = jPanel3.getGraphics();
        histograma.setColor(Color.WHITE);
        histograma.fillRect(0, 0, d.width - 1, d.height - 1);
        histograma.setColor(Color.BLACK);
        histograma.drawRect(0, 0, d.width - 1, d.height - 1);
        //vertical
        histograma.drawLine(40, 9, 40, 77);
        histograma.drawLine(41, 9, 41, 77);

        //horizontal
        histograma.drawLine(34, 70, 300, 70);
        histograma.drawLine(34, 71, 300, 71);

        long maxv = 0;
        long array[] = null;
        double dis;
        if (img != null) {
            if (c == Color.red || c == Color.gray) {
                array = img.hred;
            } else if (c == Color.green) {
                array = img.hgreen;
            } else if (c == Color.blue) {
                array = img.hblue;
            }

            for (int i = 0; i < 256; i++) {
                if (maxv < array[i]) {
                    maxv = array[i];
                }
            }
            histograma.setColor(c);

            for (int i = 0; i < 256; i++) {
                dis = (array[i] * 60) / (double) maxv;
                histograma.drawLine(42 + i, 69, 42 + i, 69 - (int) dis);
            }

        }
        histograma.setColor(Color.BLACK);
        histograma.drawRect(0, 0, d.width - 1, d.height - 1);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Interfaz().setVisible(true);
            }
        });
    }

    //******************************
    //**
    public void escalar() {

        double fancho;   //width 
        double falto;    //height

        int ancho = img.getWidthImagen();
        int alto = img.getHeightImagen();
        int newAncho = Integer.parseInt(jTextFieldAncho.getText());
        int newAlto = Integer.parseInt(jTextFieldAlto.getText());

        fancho = (float) ancho / newAncho;
        falto = (float) alto / newAlto;

        //BufferedImage imgEscale = new BufferedImage(newAlto, newAncho, BufferedImage.TYPE_INT_RGB);
        //Imagen imgEsc = new Imagen(imgEscale);
        //primero creamos un nuevo buffer/raster que tendr'a los datos de la imagen ya con el nuevo tama;o
        //luego creamos un nuevo objeto imagen, en donde le pasaremos ese buffer ya preparado
        //se dibuja y listo...
        //el proceso lala es el de crear el buffer con los nuevos datos de pixeles. 

        imgEscale = new BufferedImage(newAncho, newAlto, BufferedImage.TYPE_INT_RGB);
        //creamos la nueva imagen escalada
        WritableRaster rasterImagenEscalada = imgEscale.getRaster();

        float auxr;
        float auxg;
        float auxb;
        int newi;
        int newj;
        short[][] red = new short[newAncho][newAlto];
        short[][] green = new short[newAncho][newAlto];
        short[][] blue = new short[newAncho][newAlto];
//        Arrays.fill(red, 0);
//        Arrays.fill(green, 0);
//        Arrays.fill(blue, 0);

        for (int i = 0; i < newAncho; i++) {
            for (int j = 0; j < newAlto; j++) {
                newi = (int) (i * fancho);
                newj = (int) (j * falto);

                auxr = (float) (img.red[newi][newj]);
                auxg = (float) (img.green[newi][newj]);
                auxb = (float) (img.blue[newi][newj]);

                red[i][j] = (short) auxr;
                green[i][j] = (short) auxg;
                blue[i][j] = (short) auxb;

                rasterImagenEscalada.setSample(i, j, 0, red[i][j]);
                rasterImagenEscalada.setSample(i, j, 1, green[i][j]);
                rasterImagenEscalada.setSample(i, j, 2, blue[i][j]);

//                    hred[(int) aux]++;
//                    hgreen[(int) aux]++;
//                    hblue[(int) aux]++;
            }
        }
        escale = true;
        jLabel1.setIcon(new ImageIcon(imgEscale));
    }

    //******************************
    //**
    public void rotar(double angulo) {


        int ancho = img.getWidthImagen();
        int alto = img.getHeightImagen();

        int mitadx = (int) ancho >> 1;
        int mitady = (int) alto >> 1;

        System.out.println(Math.sin(45 * Math.PI / 180));
        double coseno, seno, nx, ny;
        double minx = 0, miny = 0, maxx = 0, maxy = 0;

        coseno = Math.cos(-angulo);
        seno = Math.sin(-angulo);

        nx = coseno * (0 - mitadx) + -seno * (0 - mitady) + mitadx;
        ny = seno * (0 - mitadx) + coseno * (0 - mitady) + mitady;
        miny = Math.min(miny, ny);
        maxy = Math.max(maxy, ny);
        minx = Math.min(minx, nx);
        maxx = Math.max(maxx, nx);

        nx = coseno * (ancho - mitadx) + -seno * (0 - mitady) + mitadx;
        ny = seno * (ancho - mitadx) + coseno * (0 - mitady) + mitady;
        miny = Math.min(miny, ny);
        maxy = Math.max(maxy, ny);
        minx = Math.min(minx, nx);
        maxx = Math.max(maxx, nx);

        nx = coseno * (0 - mitadx) + -seno * (alto - mitady) + mitadx;
        ny = seno * (0 - mitadx) + coseno * (alto - mitady) + mitady;
        miny = Math.min(miny, ny);
        maxy = Math.max(maxy, ny);
        minx = Math.min(minx, nx);
        maxx = Math.max(maxx, nx);

        nx = coseno * (ancho - mitadx) + -seno * (alto - mitady) + mitadx;
        ny = seno * (ancho - mitadx) + coseno * (alto - mitady) + mitady;
        miny = Math.min(miny, ny);
        maxy = Math.max(maxy, ny);
        minx = Math.min(minx, nx);
        maxx = Math.max(maxx, nx);


        int NewAncho = (int) (maxx - minx);
        int NewAlto = (int) (maxy - miny);

        //System.out.println("ALTO = " + NewAlto);
        //System.out.println("ANCHO = " + NewAncho);

        BufferedImage imgRotada = new BufferedImage(NewAncho, NewAlto, BufferedImage.TYPE_4BYTE_ABGR);
        //creamos la nueva imagen rotada
        WritableRaster rasterImagenRotada = imgRotada.getRaster();

        float auxr;
        float auxg;
        float auxb;
        short[][] red = new short[NewAlto][NewAncho];
        short[][] green = new short[NewAlto][NewAncho];
        short[][] blue = new short[NewAlto][NewAncho];
        short[][] alpha = new short[NewAlto][NewAncho];

        coseno = Math.cos(-angulo);
        seno = Math.sin(-angulo);

        int xc = (int) (maxx + minx - 1) >> 1;
        int yc = (int) (maxy + miny - 1) >> 1;

        //System.out.println("ACA xc " + ((int)(maxx - minx)>>1));
        //System.out.println("ACA yc " + ((int)(maxy - miny)>>1));

        for (int i = 0; i < NewAncho; i++) {
            for (int j = 0; j < NewAlto; j++) {
                nx = xc + (coseno * (i - xc) - seno * (j - yc));
                ny = yc + (seno * (i - xc) + coseno * (j - yc));

                //System.out.println(i + " + " + j + " + " + nx + " + " + ny + " + "+minx + "+" + miny + "+" + maxx + "+" + maxy);

                if (nx < 0 || nx > ancho || ny < 0 || ny > alto) {
                    rasterImagenRotada.setSample(i, j, 0, 0);
                    rasterImagenRotada.setSample(i, j, 1, 0);
                    rasterImagenRotada.setSample(i, j, 2, 0);
                    rasterImagenRotada.setSample(i, j, 3, 255);
                } else {
                    rasterImagenRotada.setSample(i, j, 0, img.red[(int) nx][(int) ny]);
                    rasterImagenRotada.setSample(i, j, 1, img.green[(int) nx][(int) ny]);
                    rasterImagenRotada.setSample(i, j, 2, img.blue[(int) nx][(int) ny]);
                    rasterImagenRotada.setSample(i, j, 3, 255);
                }//else
                // hred[(int) aux]++; // hgreen[(int) aux]++; // hblue[(int) aux]++;
            }
        }
        //System.out.println(coseno * 0 + -seno * NewAlto / 2);
        
        jLabel1.setIcon(new ImageIcon(imgRotada));
        img = new Imagen(imgRotada);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup Group_color;
    private javax.swing.ButtonGroup Group_rotar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBoxCuantizar;
    private javax.swing.JComboBox jComboBoxRotar;
    public javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelDimensiones;
    private javax.swing.JLabel jLabelPeso;
    private javax.swing.JLabel jLabelbpp;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSlider jSliderEscalar;
    private javax.swing.JTextField jTextFieldAlto;
    private javax.swing.JTextField jTextFieldAncho;
    private javax.swing.JTextField jTextFieldRotar;
    private javax.swing.JMenuItem open;
    // End of variables declaration//GEN-END:variables
    private FileManager fm;
    private Imagen img;
    private boolean escale;
    private BufferedImage imgEscale;
    private Graphics histograma;
}
